package orders

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"github.com/kuangshp/go-utils/k"
	"insighttracking/common/utils"
	"insighttracking/query/model"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/purchase/internal/svc"
	"insighttracking/apps/purchase/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetOrdersDetailByIdApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetOrdersDetailByIdApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetOrdersDetailByIdApiLogic {
	hostname, _ := os.Hostname()
	return &GetOrdersDetailByIdApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetOrdersDetailByIdApiLogic) GetOrdersDetailByIdApi(req *types.IdReq) (resp *types.OrdersResp, err error) {
	ordersBo, err := service.OrdersService.GetDetailByIdApi(l.ctx, req.Id)
	if err != nil {
		return nil, errors.New("传递的主键错误")
	}
	// 查询订单详情
	detailEntities, err := service.OrdersDetailService.GetByOrderIdListApi(l.ctx, []int64{req.Id})
	var detailList = make([]types.OrdersDetailResp, 0)
	if err == nil && len(detailEntities) > 0 {
		detailList = k.Map(detailEntities, func(item *model.OrdersDetailEntity, index int) types.OrdersDetailResp {
			return types.OrdersDetailResp{
				ID:                      item.ID,                                 // 主键id
				SpuID:                   item.SpuID,                              // 商品id,关联到spu表主键id
				SpuCount:                item.SpuCount.StringFixed(2),            // 商品数量
				SnapshotPrice:           item.SnapshotPrice.StringFixed(2),       // 快照单价
				SpuAmount:               item.SpuAmount.StringFixed(2),           // 商品金额小计
				SnapshotName:            item.SnapshotName,                       // 快照商品名称
				SnapshotProductImageURL: item.SnapshotProductImageURL,            // 快照产品图片
				SnapshotCategory:        item.SnapshotCategory,                   // 快照分类
				SnapshotUnit:            item.SnapshotUnit,                       // 快照单位
				SnapshotSpuType:         item.SnapshotSpuType,                    // 快照商品类型,0表示基础商品,1表示加工商品
				SnapshotBrand:           item.SnapshotBrand,                      // 快照品牌
				SnapshotSupplier:        item.SnapshotSupplier,                   // 快照供应商
				Remark:                  item.Remark,                             // 备注
				CreatedAt:               utils.TimestampToString(item.CreatedAt), // 创建时间
				UpdatedAt:               utils.TimestampToString(item.UpdatedAt), // 更新时间
				Status:                  item.Status,                             // 订单状态,0表示已下单(待采购),10分拣中，20已发货,30已完成,40取消订单
			}
		})
	}
	return &types.OrdersResp{
		ID:                 ordersBo.ID,                                                                               // 主键id
		OrderNo:            ordersBo.OrderNo,                                                                          // 订单流水号
		EnterpriseID:       ordersBo.EnterpriseID,                                                                     // 关联到enterprise表主键id
		CustomerID:         ordersBo.CustomerID,                                                                       // 客户id,关联到enterprise_customer表主键id
		CustomerName:       ordersBo.CustomerName,                                                                     // 门店名称
		SendGoodsDate:      k.If(ordersBo.SendGoodsDate.IsZero(), 0, utils.TimestampToString(ordersBo.SendGoodsDate)), // 发货日期
		SendGoodsMethod:    ordersBo.SendGoodsMethod,                                                                  // 发货方式,0表示司机配送
		SettlementMethod:   ordersBo.SettlementMethod,                                                                 // 结算方式，0表示账期
		PurchaserID:        ordersBo.PurchaserID,                                                                      // 采购员,关联到
		PurchaserName:      ordersBo.PurchaserName,                                                                    // 采购员名称
		Remark:             ordersBo.Remark,                                                                           // 订单备注
		TotalAmount:        ordersBo.TotalAmount.StringFixed(2),                                                       // 订单金额
		TotalCount:         ordersBo.TotalCount.StringFixed(2),                                                        // 订单商品数量
		Status:             ordersBo.Status,                                                                           // 订单状态,0表示已下单(待采购),1表示采购中,2表示已采购,3待入库，4已入库，5已发货,6已收货,7表示退货
		ReceiveGoodsName:   ordersBo.ReceiveGoodsName,                                                                 // 收货员姓名
		ReceiveGoodsMobile: ordersBo.ReceiveGoodsMobile,                                                               // 收货人手机号码
		ReceiveAddress:     ordersBo.ReceiveAddress,                                                                   // 收货人地址
		IsPurchasingPlan:   ordersBo.IsPurchasingPlan,                                                                 // 是否发布采购计划,0表示不发布,1表示发布
		Source:             ordersBo.Source,                                                                           // 订单来源,0表示app端,1表示pc下单
		SettlementStatus:   ordersBo.SettlementStatus,                                                                 // 结算状态，0表示未结算,1表示已结算
		SendGoodsID:        ordersBo.SendGoodsID,                                                                      // 配置司机,关联到account表主键id
		SendGoodsName:      ordersBo.SendGoodsName,                                                                    // 配送司机名称
		WarehouseID:        ordersBo.WarehouseID,                                                                      // 发货仓库,关联到warehouse表主键id
		WarehouseName:      ordersBo.WarehouseName,                                                                    // 仓库名称
		CreatedAt:          utils.TimestampToString(ordersBo.CreatedAt),                                               // 创建时间
		UpdatedAt:          utils.TimestampToString(ordersBo.UpdatedAt),                                               // 更新时间
		DetailList:         detailList,                                                                                // 商品详情
	}, nil
}
